package com.xunyin.modules.mtuserbalance.service.mapper;

import com.xunyin.common.mapper.CoreMapper;
import com.xunyin.modules.mtuserbalance.domain.MtUserBalance;
import com.xunyin.modules.mtuserbalance.vo.MtUserBalanceVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户门店余额 Mapper 接口
 * <p>
 * Created by xunyin
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Repository
public interface MtUserBalanceMapper extends CoreMapper<MtUserBalance> {

    @Select("SELECT sum(user_balance) as userBalance from mt_user_balance o " +
            " where o.user_id = #{userId} ")
    BigDecimal sumUserBalance(@Param("userId") Integer userId);


    //SELECT  t.store_id as storeId,t.user_balance as userBalance,t.user_id as userId,(SELECT name from mt_store where id=t.store_id) as storeName from mt_user_balance t where t.user_id = 1;
    @Select("SELECT t.store_id as storeId,t.user_balance as userBalance,t.user_id as userId,(SELECT name from mt_store where id=t.store_id) as storeName " +
            "from mt_user_balance t where t.user_id = #{userId} ")
    List<MtUserBalanceVo> getStoreBalanceList(@Param("userId") Integer userId);

    @Select("<script> select IFNULL(sum(amount),0) from mt_balance " +
            " where is_del=0 and status not in ('D') " +
            " <if test =\"storeId !=null\"> and store_id = #{storeId} </if> " +
            " <if test =\"storeIds !=null\"> and store_id in (#{storeIds}) </if> " +
            //" <if test =\"type !=null\"> and type = #{type} </if> " +
            //" <if test =\"status !=null\"> and status = #{status} </if> " +
            " <if test =\"mobile !=null\"> and mobile = #{mobile} </if> " +
            "</script>")
    Double sumUserBalanceMoney(@Param("storeId") Integer storeId,
                               @Param("storeIds") String storeIds,
                              @Param("mobile") String mobile);

}
